package com.uxhao.msmservice.controller;

import com.uxhao.commonutils.R;
import com.uxhao.commonutils.util.RandomUtil;
import com.uxhao.msmservice.service.EmailService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author uxhao
 * @date 2021/1/19 10:16
 */
@Api(tags = {"邮箱接口"})
@RestController
@RequestMapping("/msmservice/email")
//@CrossOrigin //跨域
public class EmailApiController {

    @Autowired
    private EmailService emailService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = "/sendEmail/{toEmail}")
    public R sendEmail(@PathVariable String toEmail) {
        String code = redisTemplate.opsForValue().get(toEmail);//从redis中取缓存
        if (!StringUtils.isEmpty(code)) return R.ok();

        code = RandomUtil.getFourBitRandom();//随机生成验证码
        emailService.sendEmail(toEmail, code);

        redisTemplate.opsForValue().set(toEmail, code, 5, TimeUnit.MINUTES);
        return R.ok();

    }
}
